package com.wze.wze.Controller;

import com.wze.wze.Aop.PasToken;
import com.wze.wze.Entity.LikeMusic;
import com.wze.wze.Entity.Music;
import com.wze.wze.Entity.ResultEntity;
import com.wze.wze.Entity.UserTab;
import com.wze.wze.Service.MusicService;
import com.wze.wze.Util.JSONTool;
import com.wze.wze.Util.OSSUtil;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@CrossOrigin //解决跨域
@RestController //控制器注解
@RequestMapping("/Music/")//请求路径
public class MusicController {
    @Resource
    MusicService musicService;
    ResultEntity resultEntity = null;

    @PostMapping("Music")
    private String addMusic(@RequestParam("MusicName") String MusicName, @RequestParam("mfile") MultipartFile MusicFile, @RequestParam("ifile") MultipartFile MusicIFile,
                            @RequestParam("Musict") String Musict, @RequestParam("MusicLyric") String MusicLyric, @RequestParam("Singer") int Singer) {
        resultEntity = new ResultEntity();
        OSSUtil ossUtil = new OSSUtil();
        String UserImg=null;
        String FilePath="D:\\BiShe\\";//存储Excel的位置
        String SuffixName1 = MusicFile.getOriginalFilename().substring(MusicFile.getOriginalFilename().lastIndexOf("."));//获取文件后缀
        String FileNewName1= UUID.randomUUID().toString()+SuffixName1;//新的文件名
        File dest1 = new File(FilePath+FileNewName1);//生成新的文件
        String SuffixName2 = MusicIFile.getOriginalFilename().substring(MusicIFile.getOriginalFilename().lastIndexOf("."));//获取文件后缀
        String FileNewName2= UUID.randomUUID().toString()+SuffixName2;//新的文件名
        File dest2 = new File(FilePath+FileNewName2);//生成新的文件
        if(!dest1.getParentFile().exists() && dest2.getParentFile().exists()){//如果路径不存在，就生成
            dest1.getParentFile().mkdirs();
            dest2.getParentFile().mkdirs();
        }
        try{
            MusicFile.transferTo(dest1);//将临时文件保存到对应的路径
            MusicIFile.transferTo(dest2);//将临时文件保存到对应的路径
        }catch (Exception e){
            e.printStackTrace();
        }
        String OSSFilePath1 = ossUtil.updateFile(FilePath+FileNewName1,FileNewName1);
        String OSSFilePath2 = ossUtil.updateFile(FilePath+FileNewName2,FileNewName2);
            try {
                if (musicService.addMusic(MusicName,OSSFilePath1,OSSFilePath2,Musict,MusicLyric,Singer) > 0) {
                    resultEntity.setCode("0");
                    resultEntity.setMsg("添加成功！");
                } else {
                    resultEntity.setCode("1");
                    resultEntity.setMsg("添加失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultEntity.setCode("9999");
                resultEntity.setMsg("系统错误，请联系管理员");
            }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("Music")
    private String getMusicById(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            Music music  = musicService.getMusicById(id);
            if (music == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此音乐");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(music);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);

    }

    @DeleteMapping("Music")
    private String delMusic(@RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        try {
            if (musicService.delMusicById(id)> 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }


    @PutMapping("Music")
    private String upMusic(@RequestParam("MusicName") String MusicName, @RequestParam("MusicFile") String MusicFile, @RequestParam("MusicIFile") String MusicIFile,
                          @RequestParam("Musict") String Musict, @RequestParam("MusicLyric") String MusicLyric, @RequestParam("Singer") int Singer, @RequestParam("id") int id) {
        resultEntity = new ResultEntity();
        OSSUtil ossUtil = new OSSUtil();
        String Music=null,MusicImage=null;
        if (ossUtil.updateFile(MusicFile, MusicName+".mp3")!=null) {
            Music= ossUtil.updateFile(MusicFile,MusicName+".mp3");
            MusicImage = ossUtil.updateFile(MusicIFile,MusicName+".png");
            System.out.println( "上传成功");
            try {
                Music music = new  Music();
                music.setId(id);
                music.setMusicName(MusicName);
                music.setMusic(Music);
                music.setMusicImage(MusicImage);
                music.setMusict(Musict);
                music.setMusicLyric(MusicLyric);
                music.setSid(Singer);
                if (musicService.upMusic(music)> 0) {
                    resultEntity.setCode("0");
                    resultEntity.setMsg("修改成功！");
                } else {
                    resultEntity.setCode("1");
                    resultEntity.setMsg("修改失败！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultEntity.setCode("9999");
                resultEntity.setMsg("系统错误，请联系管理员");
            }
        } else {
            System.out.println( "上传失败");
        }

        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PasToken
    @GetMapping("musicList")
    private String MusicList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("MusicNAme") String MusicName,
            @RequestParam("id") int id
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<Music> list = musicService.getMusicList(Start, limit,id,MusicName);
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PasToken
    @GetMapping("UnmusicList")
    private String UnMusicList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("Sid") int Sid,
            @RequestParam("id") int id
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<Music> list = musicService.getUnMusicList(Start, limit,id,Sid);
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }
}
